<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>API 管理</span>
      </div>
      <div class="filter-container">
        <el-input
          v-model="query.name"
          placeholder="API 名字"
          style="width: 200px"
          class="filter-item"
        />
        <el-select
          v-model="query.type"
          placeholder="API 种类"
          clearable
          style="width: 130px"
          class="filter-item"
        >
          <el-option
            v-for="item in apiTypes"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <el-select
          v-model="query.status"
          placeholder="状态"
          clearable
          style="width: 120px"
          class="filter-item"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >
          查询
        </el-button>
        <el-button
          class="filter-item"
          type="default"
          icon="el-icon-refresh"
          @click="resetFilter"
        >
          重置
        </el-button>
      </div>
      <el-table
       
        :data="apis"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="name" label="API 名字" width="180" />
        <el-table-column prop="testurl" label="测试链接" width="180" />
        <el-table-column prop="apiapply" label="接口提供方" width="180" />
        <el-table-column prop="updatetime" label="更新时间" width="180" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="category_name" label="API 种类" width="180" />
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        background
        layout="prev, pager, next"
        :total="filteredApis.length"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="handlePageChange"
      />
    </el-card>

    <!-- 编辑弹窗 -->
    <el-dialog title="编辑 API" :visible.sync="editDialogVisible">
      <el-form :model="editForm">
        <el-form-item label="API 名字" label-width="120px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="测试链接" label-width="120px">
          <el-input v-model="editForm.testLink" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="接口提供方" label-width="120px">
          <el-input v-model="editForm.provider" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="更新时间" label-width="120px">
          <el-input v-model="editForm.updateTime" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" label-width="120px">
          <el-input
            v-model="editForm.description"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="API 种类" label-width="120px">
          <el-select v-model="editForm.type" placeholder="请选择">
            <el-option
              v-for="item in apiTypes"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmEdit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import axios from "axios";

export default {
  data() {
    return {
      query: {
        name: "",
        type: "",
        status: "",
      },
      apiTypes: ["类型1", "类型2", "类型3"],
      statusOptions: ["正常", "维护", "停用"],
      apis: [], // 从后端获取的数据
      loading: false,
      editDialogVisible: false,
      editForm: {
        name: "",
        testLink: "",
        provider: "",
        updateTime: "",
        description: "",
        type: "",
      },
      currentEditIndex: -1,
      pageSize: 10,
      currentPage: 1,
    };
  },
  computed: {
    filteredApis() {
      return this.apis.filter((api) => {
        return (
          (!this.query.name || api.name.includes(this.query.name)) &&
          (!this.query.type || api.type === this.query.type) &&
          (!this.query.status || api.status === this.query.status)
        );
      });
    },
    paginatedApis() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.filteredApis.slice(start, end);
    },
  },
  methods: {
    handleFilter() {
      this.fetchApis();
    },
    resetFilter() {
      this.query.name = "";
      this.query.type = "";
      this.query.status = "";
    },
    handleEdit(row) {
      this.currentEditIndex = this.apis.indexOf(row);
      this.editForm = { ...row };
      this.editDialogVisible = true;
    },
    confirmEdit() {
      this.apis.splice(this.currentEditIndex, 1, { ...this.editForm });
      this.editDialogVisible = false;
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该API, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.apis = this.apis.filter((api) => api !== row);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handlePageChange(page) {
      this.currentPage = page;
    },
    fetchApis() {
        console.log(localStorage.getItem("token"));
      this.loading = true;
      const tenantId = "yourTenantId"; // 替换为实际的租户ID
      axios
        .get("/api/system/apis", {
          params: {
            tenantId: 1,
            name: this.query.name,
            type: this.query.type,
            status: this.query.status,
          },
        })
        .then((response) => {
          this.apis = response.data.data;
          console.log("获取API数据成功", this.apis);
        })
        .catch((error) => {
          console.error("获取API数据失败", error);
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
  mounted() {
    this.fetchApis();
  },
};
</script>
  
  <style scoped>
.filter-container {
  margin-bottom: 20px;
}
.filter-item {
  margin-right: 10px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
  